package com.squid.user.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.squid.base.common.bean.PageReq;
import com.squid.base.common.bean.PageResp;
import com.squid.base.common.enums.CommonErrorEnums;
import com.squid.base.common.enums.DeleteTokenEnums;
import com.squid.base.common.enums.EnableTokenEnums;
import com.squid.base.common.error.ExceptionFactory;
import com.squid.base.common.mapper.EasyServiceImpl;
import com.squid.base.utils.LimitUtils;
import com.squid.user.convertor.UserConvertor;
import com.squid.user.model.entity.UserEntity;
import com.squid.user.model.mapper.UserEntityMapper;
import com.squid.user.model.req.UserEntityRegisterReq;
import com.squid.user.model.req.UserEntitySelectReq;
import com.squid.user.model.req.UserEntityUpdateReq;
import com.squid.user.model.resp.UserEntitySelectResp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;



/**
* @author jinhang1
* @description 针对表【user_entity(用户信息表)】的数据库操作Service实现
* @createDate 2024-02-07 15:36:23
*/
@Service
@Slf4j
public class UserEntityServiceImpl extends EasyServiceImpl<UserEntityMapper, UserEntity>
    implements UserEntityService{
    @Resource
    UserEntityMapper userEntityMapper;
    @Override
    public void register(UserEntityRegisterReq req) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(req.getUsername());
        userEntity.setPassword(req.getPassword());
        repeat(userEntity);
        try {
            this.save(userEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1005, e);
        }
    }
    private void repeat(UserEntity userEntity){
        UserEntity userRepeat = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUsername, userEntity.getUsername())
                .eq(UserEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                .eq(UserEntity::getEnableToken, EnableTokenEnums.Y.getCode())
        );
        if (!ObjectUtils.isEmpty(userRepeat)) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1001);
        }
    }
    @Override
    public void updateInfo(UserEntityUpdateReq req) {
        UserEntity userEntity = UserConvertor.INSTANCE.to(req);
        try {
            this.updateById(userEntity);
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1002, e);
        }
    }

    @Override
    public PageResp<UserEntitySelectResp> pageInfo(PageReq<UserEntitySelectReq> req) {
        Page<UserEntitySelectResp> page = new Page<>(req.getPageNum(), req.getPageSize());
        try {
            IPage<UserEntitySelectResp> response = userEntityMapper.page(page, req.getRequest());
            return LimitUtils.formatResponse(response);
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1003, e);
        }
    }

    @Override
    public void deleteUser(String id) {
        try {
            this.update(new LambdaUpdateWrapper<UserEntity>()
                    .eq(UserEntity::getId, id)
                    .set(UserEntity::getDeleteToken, DeleteTokenEnums.Y.getCode())
            );
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1004, e);
        }
    }

    /**
     * 按用户名查询用户
     *
     * @param username 用户名
     * @return
     */
    public UserEntity queryUserByUsername(String username) {
        try {
            return this.getOne(new LambdaUpdateWrapper<UserEntity>()
                    .eq(UserEntity::getUsername, username)
                    .eq(UserEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                    .set(UserEntity::getEnableToken, EnableTokenEnums.Y.getCode())
            );
        } catch (Exception e) {
            throw ExceptionFactory.instanceBusinessException(CommonErrorEnums.CODE_1003, e);
        }
    }
}




